package com.orm.dao;

import com.orm.domain.User;
import com.orm.utils.HibernateUtils;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import java.util.List;

/**
 * 用户的增删改查功能
 **/
public class UserDao {
    /**
     * 添加用户
     * 传入user数据
     */
    public void addUser(User user) {
        //加载资源
        Session session = HibernateUtils.getCurrentSession();
        //开启事务，这个一般是在service层，但是这里为了测试方便，没有写service
        Transaction transaction = session.beginTransaction();
        try {
            session.save(user);
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
        } finally {
            session.close();
        }
    }

    /**
     * 查找用户
     * get
     * load
     */
    public User queryUser(int id) {
        Session session = HibernateUtils.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        User user = null;
        try {
            //user = session.get(User.class, 1);
            user = session.load(User.class, 1);
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
        } finally {
            session.close();
        }
        return user;
    }

    /**
     * 查找所有用户
     * HQL语言
     * ==>createQuery
     * ==>createCriteria Hibrnate独创的面向对象查询,可以加条件
     * ==>list返回多行
     * ==>uniqueResult返回一行
     * ==>分页 limit index,count;   -->setFirstResult()从第几个开始  setMaxResults()拿几个数据
     */
    public List<User> queryAllUser() {
        Session session = HibernateUtils.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        List<User> list = null;
        try {
            //createQuery,传入hql语句查询
            //select * from t_user
            Query query = session.createQuery("from com.orm.domain.User");
            query.setFirstResult(2);
            query.setMaxResults(1);
            //list将hql语句执行，并返回多行结果
            list = query.list();
            //uniqueResult将hql语句执行，并返回一行结果
            //User user = (User) query.uniqueResult();

            //criteria查询==================================================================
           /* Criteria criteria = session.createCriteria(User.class);
            list = criteria.list();*/

            //原生sql查询===================================================================
           /* NativeQuery sqlQuery = session.createSQLQuery("select * from t_user ");
            //把查询结果封装到对象中
            sqlQuery.addEntity(User.class);
            list=sqlQuery.list();*/
            /*List<Object[]> list1= sqlQuery.list();
            for(Object[] obj:list1){
                System.out.println(Arrays.toString(obj));
            }*/


            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
        } finally {
            session.close();
        }
        return list;
    }

    /**
     * 修改用户，先得到用户，然后修改用户信息，然后更新
     */
    public User updateUser() {
        Session session = HibernateUtils.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        User user = null;
        try {
            user = session.get(User.class, 1);
            user.setUsername("张浩轩");
            session.update(user);
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
        } finally {
            session.close();
        }
        return user;
    }

    /**
     * 删除用户
     */
    public void deleteUser() {
        Session session = HibernateUtils.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        try {
            User user = new User();
            user.setUid(1);//主要是为了获取到id，因为删除需要根据id来删除
            session.delete(user);
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
        } finally {
            session.close();
        }
    }
}
